﻿#region Usings

using System;
using System.Globalization;
using System.Linq;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using ScrumTable.UI.View.Util;

#endregion

namespace ScrumTable.UI.View.Ink
{
    /// <summary>
    /// ScrumStroke
    /// </summary>
    public class TextStroke : ScrumStroke
    {

        #region Declarations
        //--------------------------------------------------------------------
        // Declarations
        //--------------------------------------------------------------------

        private FormattedText _formattedText;
        private double _fontSize;
        
        #endregion

        #region Properties
        //--------------------------------------------------------------------
        // Properties
        //--------------------------------------------------------------------
        public string Text { get; set; }

        #endregion

        #region Constructors / Destructor
        //--------------------------------------------------------------------
        // Constructors / Destructor
        //--------------------------------------------------------------------

        public TextStroke(Point from, Point to, DrawingAttributes attributes) : base(new StylusPointCollection(new [] { from, to }), attributes) { }

        private TextStroke(StylusPointCollection collection, DrawingAttributes attributes)
            : base(collection, attributes)
        {
            
        }
        #endregion




        #region Methods
        protected override void DrawCore(DrawingContext drawingContext, DrawingAttributes drawingAttributes)
        {
            Point to = To;
            Point from = From;
            CalcFontSize(to, from);
            _formattedText = new FormattedText(
                Text,
                CultureInfo.GetCultureInfo("en-us"),
                FlowDirection.LeftToRight,
                new Typeface("Segoe UI"),
                _fontSize,
                new SolidColorBrush(drawingAttributes.Color));

            //remove the space between the lines
            drawingContext.DrawText(_formattedText, new Point(from.X, from.Y /*- _formattedText.Extent/2*/));
        }

        private void CalcFontSize(Point to, Point from)
        {
            _fontSize = Math.Min(Math.Max(1, FontUtil.GetFontSize(Math.Abs(to.Y - from.Y), Math.Abs(to.X - from.X), Text)), 400);
        }

        protected override void OnStylusPointsChanged(EventArgs e)
        {
            base.OnStylusPointsChanged(e);
            CalcFontSize(To, From);
            _formattedText.SetFontSize(_fontSize);
            Pack();
        }
        protected override StylusPointCollection Pack(Point from, Point to, double width, double height, StylusPointCollection collection)
        {

            height = _formattedText.Height;
            width = _formattedText.Width;

            //create a rectlange
            collection.Add(new StylusPoint(from.X, from.Y));
            collection.Add(new StylusPoint(from.X + width , from.Y));
            collection.Add(new StylusPoint(from.X + width, from.Y + height));
            collection.Add(new StylusPoint(from.X, from.Y + height));
            collection.Add(new StylusPoint(from.X, from.Y));
            
            //needs for a correct rectangle end
            collection.Add(new StylusPoint(from.X + width, from.Y));
            //adds the last point
            collection.Add(new StylusPoint(from.X + width, from.Y + height));
            return collection;
        }
       
        #endregion
        #region Events

        #endregion
    }
}
